package com.trip.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
*
 * @author hc24
 *
 */

public class Page<E> implements Serializable{
    private static final long serialVersionUID = 9030682628072361206L;
    public static final int DEFAULT_PAGE_SIZE=10;

    /** data rows */
    private List<E> rows;

    private int start;

    private int pageSize;

    private long total;


    public Page() {
        this(new ArrayList<E>(), 0, DEFAULT_PAGE_SIZE,0);
    }

    public Page(List<E> rows, int start, int pageSize,long total) {
        this.pageSize = 10;

        if (rows == null) {
            this.rows = new ArrayList<E>();
        } else if (rows.size() <= pageSize) {
            this.rows = rows;
        } else {
            this.rows = new ArrayList<E>();
            for (int i = start; (i < start + pageSize) && (i < rows.size()); ++i)
                this.rows.add(rows.get(i));
        }
        this.start = start;
        this.pageSize = ((pageSize > 0) ? pageSize : DEFAULT_PAGE_SIZE);
        this.total=total;
    }


    public int getStart() {
        return start;
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<E> getRows() {
        return this.rows;
    }

    public void setRows(List<E> rows){
        this.rows=rows;
    }

    public static int getStartOfPage(int pageNo) {
        return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
    }

    public static int getStartOfPage(int pageNo, int pageSize) {
        return Math.max((pageNo - 1) * pageSize, 0);
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void addRow(E row) {
        if(rows==null)
            rows=new ArrayList<>();

    }
}
